package cn.yuanqiao.web.controller.system;

import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.system.domain.YqCyxz;
import cn.yuanqiao.system.service.IYqCyxzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 常用下载Controller
 * 
 * @author sy
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/system/yqCyxz")
public class YqCyxzController extends BaseController
{
    @Autowired
    private IYqCyxzService yqCyxzService;

    /**
     * 查询常用下载列表
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:list')")
    @GetMapping("/list")
    public TableDataInfo list(YqCyxz yqCyxz)
    {
        LoginUser user = SecurityUtils.getLoginUser();

        if(user == null){
            user = new LoginUser();
            user.setUserId(1L);
        }

        yqCyxz.setUserId(user.getUserId());
        yqCyxz.setReleaseScope(user.getSysUser().getDeptId().toString());
        startPage();
//        List<YqCyxz> list = yqCyxzService.selectYqCyxzList(yqCyxz.getTableName(),user.getUserid(),user.getSysUser().getDeptId());
        List<YqCyxz> list = yqCyxzService.selectYqCyxzList(yqCyxz);
        return getDataTable(list);
    }

    /**
     * 导出常用下载列表
     */
//    @RequiresPermissions("system:yqcyxz:export")
//    @Log(title = "常用下载", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, YqCyxz yqCyxz)
//    {
//        List<YqCyxz> list = yqCyxzService.selectYqCyxzList(yqCyxz);
//        ExcelUtil<YqCyxz> util = new ExcelUtil<YqCyxz>(YqCyxz.class);
//        util.exportExcel(response, list, "常用下载数据");
//    }

    /**
     * 获取常用下载详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqCyxzService.selectYqCyxzByID(ID));
    }

    /**
     * 新增常用下载
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:add')")
    @Log(title = "常用下载", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody YqCyxz yqCyxz)
    {
        return toAjax(yqCyxzService.insertYqCyxz(yqCyxz));
    }

    /**
     * 上传文件
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:upload')")
    @Log(title = "常用下载", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam(value="mj",required=false)String mj)
    {
        return toAjax(yqCyxzService.upload(file,mj));
    }

    /**
     * 修改常用下载
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:edit')")
    @Log(title = "常用下载", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody YqCyxz yqCyxz)
    {
        return toAjax(yqCyxzService.updateYqCyxz(yqCyxz));
    }

    /**
     * 发布文件
     */
//    @RequiresPermissions("system:yqCyxz:release")
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @Log(title = "常用下载", businessType = BusinessType.UPDATE)
    @PutMapping("/release")
    public AjaxResult release(@RequestBody YqCyxz yqCyxz)
    {
        return toAjax(yqCyxzService.release(yqCyxz));
    }

    /**
     * 取消发布
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:disRelease')")
    @Log(title = "常用下载", businessType = BusinessType.UPDATE)
    @PutMapping("/disRelease")
    public AjaxResult disRelease(@RequestBody YqCyxz yqCyxz)
    {
        return toAjax(yqCyxzService.disRelease(yqCyxz));
    }

    /**
     * 下载
     * @param
     * @param response
     * @throws Exception
     */
//    @RequiresPermissions("system:yqCyxz:download")
//    @Log(title = "下载", businessType = BusinessType.DOWNLOAD)
//    @GetMapping("/download/{IDs}")
//    public void download(@PathVariable Long[] IDs, HttpServletResponse response)throws Exception{
//        yqCyxzService.download(IDs,response);
//    }

    /**
     * 下载
     * @param
     * @param response
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:download')")
    @Log(title = "下载", businessType = BusinessType.DOWNLOAD)
    @PostMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String[] a = String.valueOf(request.getParameter("IDs")).split(",");
        String sydm = String.valueOf(request.getParameter("sydm"));
        if ("null".equals(sydm)){
            sydm = "";
        }
        Long[] b = new Long[a.length];
        for (int i = 0; i < a.length; i++) {
            b[i]=Long.valueOf(a[i]);
        }
        yqCyxzService.download(b,sydm,response);
    }

    /**
     * 删除常用下载
     */
    @PreAuthorize("@ss.hasPermi('system:yqCyxz:remove')")
    @Log(title = "常用下载", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqCyxzService.deleteYqCyxzByIDs(IDs));
    }
}
